package HTMLParser.HTMLTag;

import Parser.Token;

/*******************************************************************************
 * This class deals with Image tags. Its main function is to extract and store
 * the URL variable from an <a href="....."> tag. It stores the path to the link.
 * @author James Johnstone
 * @version FINAL
 *******************************************************************************/
public class Link extends Tag {

    private String linkAddress;

    /***************************************************************************
     * Constructor for the Link class. Takes one variable which is a Token
     * representing the HTML Tag (example: <a href="blah.html">). It then
     * extracts the link location, and stores it appropriately.
     * @param t A Token representing a HTML link tag.
     ***************************************************************************/
    public Link(Token t) {
        super(t, "Link");
        String tokenString = t.getToken().toLowerCase();
        if (tokenString.contains("a href=")) {
            int first = tokenString.indexOf("a href=" + '"') + 8;
            int last = tokenString.indexOf("" + '"', first);
            linkAddress = tokenString.substring(first, last);
        } else {
            errorLog.logError("No URL in the link tag " + t.getToken());
        }
    }

    /***************************************************************************
     * Returns whatever we have stored as the link address.
     *
     * @return A String representing the link address from the token.
     ***************************************************************************/
    public String getLink() {
        return linkAddress;
    }
}
